/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CheckInGUI.java
 *
 * Created on 26/06/2010, 03:26:18 PM
 */

package frontera;

import control.CheckInCTR;
import control.ClientesCTR;
import control.ServiciosCTR;
import entidad.Servicios;
import entidad.Clientes;
import entidad.Movimientos;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import java.util.Date;
import frontera.FramePrincipal1;


public class CheckInGUI extends javax.swing.JPanel {
    public CheckInCTR control = new CheckInCTR();
    public ClientesCTR controlCliente = new ClientesCTR();
    public ArrayList<Servicios> servicios;//Almacena la lista de servicios
    public Clientes cliente;//Almacena el Cliente seleccionado

    /** Creates new form CheckInGUI */
    public CheckInGUI() {
        initComponents();
        fechaEntrada.setText(new Date().toString());
        cargarListaServicios(1);//Cargar Lista de Habitaciones
    }

    /**
     * cargarListaServicios
     * Carga la lista de Servicios disponibles
     * dependiendo del Tipo seleccionado (Habitaciones)
     */
    private void cargarListaServicios(int id){
        ListaServicios.removeAllItems();
        servicios = FramePrincipal1.sistema.getServicios();
        for(Servicios u : servicios){
            if(u.getTipoServicio().getId()==id && u.isEstado()){
                ListaServicios.addItem(u.getNombre());
                //System.out.println(u.getNombre());
            }
        }
    }

    /**
     * buscarCliente
     * Busca un cliente y visualiza el nombre en la interfaz
     */
    private void buscarCliente(){
        CheckInCTR checkin = new CheckInCTR();
        if(!documento.getText().trim().equals("")){

            String mensaje = checkin.verificarDatos(documento.getText().trim());
            if (mensaje!="") {
                JOptionPane.showMessageDialog(this, mensaje);
            }
            Clientes c = controlCliente.getclienteByCC(Integer.parseInt(documento.getText().trim()));
            if(c!=null){
                nombreCliente.setText(c.getNombre());
            //}else{
            //    JOptionPane.showMessageDialog(this, "El Cliente no exíste!!!");
            }
        }else JOptionPane.showMessageDialog(this, "Digite el número de cedula del cliente");
    }

    /**
     * guardarCheckIn
     * Genera el CheckIn de un Cliente
     */
    private void guardarCheckIn(){
        ClientesCTR conC = new ClientesCTR();
        ServiciosCTR con = new ServiciosCTR();

        String serv = ListaServicios.getSelectedItem().toString();
        Servicios s = con.getServicioByNombre(serv);//El servicio
        Clientes c = conC.getclienteByCC(Integer.parseInt(documento.getText()));

        s.setEstado(false);

        if(control.validarCheckIn(c)){
            control.abrirCheckin(c, s);
            JOptionPane.showMessageDialog(this, "El cliente se registró correctamente en el Hotel.");
        }else JOptionPane.showMessageDialog(this, "El Cliente ya se encuentra registrado en el Hotel.");

        for(Movimientos m : FramePrincipal1.sistema.getMovimientos()){
            System.out.println(m.getClientes().getNombre()+" - "+m.getFechaIni());
        }

        this.limpiarFormulario();
    }

    /**
     * Evento al seleccionar la lista de Servicios (Habitaciones)
     * Calcula el valor unitario y valor total
     */
    private void seleccionListaServicios(){
        Servicios s = servicios.get(ListaServicios.getSelectedIndex());
        valorTotal.setText(Float.toString(control.calcularTotal(1, s.getIva(), s.getValorUnitario())));
        valorUnitario.setText(Integer.toString(s.getValorUnitario()));
    }

    /**
     * limpiarFormulario
     * Limpia los campos del formulario
     * despues de haber generado el CheckIn
     */
    private void limpiarFormulario(){
        documento.setText("");
        nombreCliente.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        nombreCliente = new javax.swing.JFormattedTextField();
        jLabel11 = new javax.swing.JLabel();
        documento = new javax.swing.JFormattedTextField();
        buscarCliente = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        guardarMovimiento = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        valorTotal = new javax.swing.JLabel();
        valorUnitario = new javax.swing.JTextField();
        ListaServicios = new javax.swing.JComboBox();
        fechaEntrada = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel7 = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(360, 396));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel3.setForeground(new java.awt.Color(0, 102, 51));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("CHECK-IN");

        jLabel12.setText("Nombre del Cliente");

        nombreCliente.setEditable(false);
        nombreCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nombreClienteActionPerformed(evt);
            }
        });

        jLabel11.setText("Documento Cliente");

        documento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                documentoActionPerformed(evt);
            }
        });

        buscarCliente.setText("Buscar");
        buscarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarClienteActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel4.setForeground(new java.awt.Color(0, 102, 51));
        jLabel4.setText("Alojamiento");

        guardarMovimiento.setText("Guardar Entrada");
        guardarMovimiento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarMovimientoActionPerformed(evt);
            }
        });

        jLabel5.setText("Habitacion");

        jLabel14.setText("Fecha de Entrada");

        jLabel9.setText("Valor Unitario");

        jLabel16.setText("Valor Total");

        valorTotal.setFont(new java.awt.Font("Tahoma", 1, 10));
        valorTotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        valorTotal.setText("$ 0");

        valorUnitario.setEditable(false);
        valorUnitario.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        ListaServicios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ListaServiciosActionPerformed(evt);
            }
        });

        fechaEntrada.setEditable(false);
        fechaEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fechaEntradaActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel2.setForeground(new java.awt.Color(0, 102, 51));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Sistema de Facturación Hotelero");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel7.setForeground(new java.awt.Color(0, 102, 51));
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("La Potra Hotel Campestre");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(nombreCliente, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(documento, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buscarCliente))
                    .addComponent(jLabel4)
                    .addComponent(guardarMovimiento, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel14)
                            .addComponent(jLabel9)
                            .addComponent(jLabel16))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(valorTotal, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(valorUnitario, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                                        .addComponent(fechaEntrada, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                                        .addComponent(ListaServicios, javax.swing.GroupLayout.Alignment.LEADING, 0, 257, Short.MAX_VALUE))
                                    .addComponent(valorTotal)))
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                            .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(documento, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(buscarCliente)))
                        .addGap(0, 0, 0))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(guardarMovimiento)
                        .addGap(110, 110, 110))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(documento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buscarCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombreCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(ListaServicios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(fechaEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(valorUnitario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(valorTotal)
                    .addComponent(jLabel16))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(guardarMovimiento)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void nombreClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nombreClienteActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_nombreClienteActionPerformed

    private void documentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_documentoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_documentoActionPerformed

    private void buscarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarClienteActionPerformed
        this.buscarCliente();
}//GEN-LAST:event_buscarClienteActionPerformed

    private void guardarMovimientoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarMovimientoActionPerformed
        this.guardarCheckIn();
}//GEN-LAST:event_guardarMovimientoActionPerformed

    /**
     * Evento al seleccionar la lista de Servicios (Habitaciones)
     * Calcula el valor unitario y valor total
     */
    private void ListaServiciosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ListaServiciosActionPerformed
        this.seleccionListaServicios();
}//GEN-LAST:event_ListaServiciosActionPerformed

    private void fechaEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fechaEntradaActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_fechaEntradaActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ListaServicios;
    private javax.swing.JButton buscarCliente;
    private javax.swing.JFormattedTextField documento;
    private javax.swing.JFormattedTextField fechaEntrada;
    private javax.swing.JButton guardarMovimiento;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JFormattedTextField nombreCliente;
    private javax.swing.JLabel valorTotal;
    private javax.swing.JTextField valorUnitario;
    // End of variables declaration//GEN-END:variables

}
